package ext.acae2.dms.parse.wc9;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

import ext.acae2.dms.bean.PartBean;
import ext.acae2.util.CmIniFileUtil;
import ext.acae2.util.LoadingXmlUtil;
import ext.acae2.util.StringUtil;

public class ParseWTPart {

	@SuppressWarnings("unchecked")
	public static PartBean execute(String path, String attrPathFolder) throws Exception{
		PartBean partBean = new PartBean();
		Map<String, String> map = new HashMap<String, String>();
		System.out.println("path"+path);
		Document doc = LoadingXmlUtil.executeForDom4j(path);
		
		Element localIDNode = (Element) doc.selectSingleNode("/WTPart/ObjectID");
		String localId = localIDNode.elementText("localId");
		System.out.println("localId"+localId);
		String partId = localId.substring(localId.lastIndexOf(":") + 1, localId.length());
		partBean.setId(partId);
		
		String masteredText = doc.getRootElement().elementText("masterUfid");
		String masterId = masteredText.substring(masteredText.indexOf(":")+1,masteredText.indexOf("|"));
		partBean.setMasterId(masterId);
		
		Node numberNode = doc.selectSingleNode("/WTPart/number");
		String number = numberNode.getText();
		partBean.setNumber(number);
		
		Node nameNode = doc.selectSingleNode("/WTPart/name");
		String name = nameNode.getText();
		partBean.setName(name);
		
		Node partTypeNode = doc.selectSingleNode("/WTPart/partType");
		String partType = partTypeNode.getText();
		partBean.setType(partType);
		
		Node versionIdNode = doc.selectSingleNode("/WTPart/versionInfo/versionId");
		String versionId = versionIdNode.getText();
		Node iterationIdNode = doc.selectSingleNode("/WTPart/versionInfo/iterationId");
		String iterationId = iterationIdNode.getText();
		String version = versionId + "." + iterationId;
		partBean.setVersion(version);
		
		List<Node> ibaPathNodes = doc.selectNodes("/WTPart/iba/ibaPath");
		List<Node> ibaValueNodes = doc.selectNodes("/WTPart/iba/ibaValue");
		for (int i = 0; i < ibaPathNodes.size(); i++) {
			String title = ibaPathNodes.get(i).getText();
			String value = ibaValueNodes.get(i).getText();
			if(!StringUtil.isEmpty(value)){
				map.put(title, value);
			}
		}
		
		ibaPathNodes = doc.selectNodes("/WTPart/excludedIba/ibaPath");
		ibaValueNodes = doc.selectNodes("/WTPart/excludedIba/ibaValue");
		for (int i = 0; i < ibaPathNodes.size(); i++) {
			String title = ibaPathNodes.get(i).getText();
			String value = ibaValueNodes.get(i).getText();
			if(!value.equals("((null))")){
				map.put(title, value);
			}
		}
		
		Map<String, String> resultMap = new HashMap<String, String>();
		CmIniFileUtil ini = new CmIniFileUtil(attrPathFolder);
		 
		Set<String> keys = ini.getSection("Attr Requird").keySet();
		for(String key:keys){
			String tcAttrName = ini.getSection("Attr Requird").get(key, "");
			Set<String> attrValueKeys = ini.getSection(key).keySet();
			if(attrValueKeys.size() != 0){
				for(String attrValueKey:attrValueKeys){
					String tcAttrValue = ini.get(attrValueKey, "");
					if(map.get(tcAttrName).equals(tcAttrValue)){
						resultMap.put(key, attrValueKey==null?"":attrValueKey);
					}
				}
			}else{
				resultMap.put(key, map.get(tcAttrName)==null?"":map.get(tcAttrName));
			}
		}
		
		partBean.setMap(resultMap);
		return partBean;
	}
	
}
